import { service } from "@/axios/index";

export const getAllDeviceTimingList = (params) => {
  let url = "/deviceTimingRule/findAllDeviceTimingRule";
  return service.get(url, { params: params }).then((response) => {
    return response.data.data;
  });
};

export const getAllPerationDeviceList = (params) => {
  let url = "/operationDevice/queryOperationDeviceAllList";
  return service.get(url, { params: params }).then((response) => {
    return response.data.data;
  });
};

// 停用和启用
export const upStateTimingInterface = (p, num) => {
  // 0是停用，1是启用
  let newParams = {
    deviceFid: p.deviceFid,
    deviceTimingRule: {
      id: p.id,
      deviceFid: p.deviceFid,
      projectId: p.projectId,
      ruleTitle: p.ruleTitle,
      gatewayType: p.gatewayType,
      startTime: p.startTime,
      stopTime: p.stopTime,
      startTimeUnsigned: p.startTimeUnsigned,
      stopTimeUnsigned: p.stopTimeUnsigned,
      state: p.state,
      enableState: num,
      chooseWorkDay: p.chooseWorkDayStr,
      operateInfo: p.operateInfoStr,
      hasPeopleBrightness: p.hasPeopleBrightness,
      noPeopleBrightness: p.noPeopleBrightness,
      operateTrigger: p.operateTrigger,
      creator: p.creator,
      nextExecutionTime: p.nextExecutionTime,
      createTime: p.createTime,
      updateTime: p.updateTime,
      skipHoliday: p.skipHoliday,
    },
  };
  let url = "/deviceTimingRule/update";
  return service.post(url, newParams).then((response) => {
    return response.data;
  });
};

// 测试
export const testTimingInterface = (p) => {
  let newParams = {
    deviceFid: p.deviceFid,
    deviceTimingRule: {
      id: p.id,
      deviceFid: p.deviceFid,
      ruleTitle: p.ruleTitle,
      gatewayType: p.gatewayType,
      startTime: p.startTime,
      stopTime: p.stopTime,
      startTimeUnsigned: p.startTimeUnsigned,
      stopTimeUnsigned: p.stopTimeUnsigned,
      state: p.state,
      enableState: p.enableState,
      chooseWorkDay: p.chooseWorkDayStr,
      operateInfo: p.operateInfoStr,
      hasPeopleBrightness: p.hasPeopleBrightness,
      noPeopleBrightness: p.noPeopleBrightness,
      operateTrigger: p.operateTrigger,
      creator: p.creator,
      nextExecutionTime: p.nextExecutionTime,
      createTime: p.createTime,
      updateTime: p.updateTime,
      skipHoliday: p.skipHoliday,
    },
  };
  console.log("testScen->params->", newParams);
  let url = "/deviceTimingRule/testScheduledTasks";
  return service.post(url, newParams).then((response) => {
    return response.data;
  });
};

// 获取表格数据
export const queryOperationDeviceAllList = (params) => {
  let url = "/operationDevice/queryOperationDeviceAllList";
  return service.get(url, { params: params }).then((response) => {
    return response.data;
  });
};

// 获取设备地区
export const getDeviceAreaAllList = (params) => {
  let url = "/operationDevice/deviceAreaAllList";
  return service.post(url, params).then((response) => {
    return response.data;
  });
};

// 获取设备别名(组)
export const getDeviceAliasAllList = (params) => {
  let url = "/operationDevice/deviceGroupAllList";
  return service.post(url, params).then((response) => {
    return response.data;
  });
};

// 新增定时情景
export const upMeshTimingRule = (pid, p, isAdd, projectId) => {
  let url = isAdd ? "/deviceTimingRule/addMeshTimingRule" : "/deviceTimingRule/update";
  let upparams = {
    deviceFid: pid,
    deviceTimingRule: {
      id: p.id,
      deviceFid: pid,
      chooseWorkDay: p.chooseWorkDay.join(),
      startTime: p.startTime,
      operateInfo: JSON.stringify(p.operateInfoObj),
      ruleTitle: p.ruleTitle,
      skipHoliday: p.skipHoliday,
      projectId: projectId,
      operateTrigger: p.operateTrigger,
      noPeopleBrightness: p.operateTrigger,
      hasPeopleBrightness: p.operateTrigger,
      gatewayType: p.gatewayType,
      createdAt: p.createdAt,
      creator: p.creator,
      deviceName: p.deviceName,
      lastExecutionTime: p.lastExecutionTime,
      updatedAt: p.updatedAt,
      enableState: p.enableState,
    },
  };
  let addparams = {
    deviceFid: pid,
    deviceTimingRule: {
      chooseWorkDay: p.chooseWorkDay.join(),
      operateInfo: JSON.stringify(p.operateInfoObj),
      startTime: p.startTime,
      ruleTitle: p.ruleTitle,
      enableState: "1",
      projectId: projectId,
    },
  };
  let params = isAdd ? addparams : upparams;
  return service.post(url, params).then((response) => {
    return response.data;
  });
};

// 获取自动新增灯状态
export const getAutoLightData = (projectId) => {
  let url = "/operation/getAutoAddLightStatus";
  let params = { deviceFid: projectId };
  return service.post(url, params).then((response) => {
    return response.data;
  });
};

// 更新自动新增灯状态
export const upAutoAddLight = (params) => {
  let url = "/operation/autoAddLight";
  return service.post(url, params).then((response) => {
    return response.data;
  });
};
